package com.salesmanager.core.business.category.dao;

import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;

import com.salesmanager.core.business.category.model.Category;
import com.salesmanager.core.business.category.model.CategoryCriteria;
import com.salesmanager.core.business.category.model.QCategory;
import com.salesmanager.core.business.generic.dao.SalesManagerEntityDaoImpl;
import com.salesmanager.core.business.generic.exception.ServiceException;
import org.hibernate.ejb.QueryImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.Query;
import java.util.List;


@Repository("categoryDao")
public class CategoryDaoImpl extends SalesManagerEntityDaoImpl<Long, Category> implements CategoryDao {
	private   Logger LOGGER = LoggerFactory.getLogger(CategoryDaoImpl.class);

	public CategoryDaoImpl() {
		super();
	}


	@Override
	public List<Category> listByDepth(int depth) {
		QCategory qCategory = QCategory.category;

		JPQLQuery query = new JPAQuery (getEntityManager());

		query.from(qCategory)
				.where(qCategory.depth.eq(depth))
				.orderBy(qCategory.sortOrder.asc(), qCategory.lineage.asc(), qCategory.lineage.asc(), qCategory.depth.asc());

		return query.distinct().list(qCategory);
		//.listDistinct(qCategory);
	}


	@Override
	public Category getByCode(String code) throws ServiceException {
        StringBuilder qs = new StringBuilder();
        qs.append("select c from Category as c ");
        qs.append("where c.code =:code");

        String hql = qs.toString();
        Query q = super.getEntityManager().createQuery(hql);

        q.setParameter("code", code);
        Category category = null;
        try {
            category = (Category)q.getSingleResult();
        } catch (javax.persistence.NoResultException e){

        }
        return category;
	}

    @Override
    public List<Category> listByLineage(String lineage) {
        QCategory qCategory = QCategory.category;

        JPQLQuery query = new JPAQuery (getEntityManager());

        query.from(qCategory)
                .where(qCategory.lineage.like(new StringBuilder().append(lineage).append("%").toString()))
                .orderBy(qCategory.sortOrder.asc(),qCategory.lineage.asc(), qCategory.lineage.asc(), qCategory.depth.asc());

        return query.distinct().list(qCategory);
    }

    @Override
    public List<Category> list(CategoryCriteria criteria) throws ServiceException {

        StringBuilder sql = new StringBuilder();
        sql.append("select c from Category c where 1=1 ");
//        sql.append("left join fetch c.parent cp ");
        if (criteria.getStatus() != null){
            sql.append("and c.categoryStatus = :status ");
        }
        sql.append("order by c.depth asc,c.id asc");

        Query query = super.getEntityManager().createQuery(sql.toString());

        if (criteria.getStatus() != null){
            query.setParameter("status",criteria.getStatus());
        }

        QueryImpl hs;
        org.hibernate.Query hq;
        List<Category> list;
        if (query instanceof QueryImpl){
            hs = (QueryImpl)query;
            hq = hs.getHibernateQuery();
//            hq.setCacheable(true);//使用二级缓冲
            list = hq.list();
        } else {
            list = query.getResultList();
        }

        return list;
    }

}
